using COW;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using static UnityEngine.UI.Image;

public class KLine : MonoBehaviour
{
    [SerializeField] Image mBody;
    [SerializeField] Image mUp;
    [SerializeField] Image mDown;

    public void SetData(KLineData line, float ydelta, int i, Vector3 origin, float lowest)
    {
        var centerw = (line.open + line.close) / 2f;
        var is_long = line.close > line.open;
        var body = Mathf.Abs(line.close - line.open);
        mBody.color = is_long ? Color.red : Color.green;
        mUp.color = is_long ? Color.red : Color.green;
        mDown.color = is_long ? Color.red : Color.green;


        var pos_close = origin + new Vector3(80 * (1 + i), (line.close - lowest) / ydelta * 80, 0) * 0.01f;
        var pos_open = origin + new Vector3(80 * (1 + i), (line.open - lowest) / ydelta * 80, 0) * 0.01f;
        var pos_high = origin + new Vector3(80 * (1 + i), (line.high - lowest) / ydelta * 80, 0) * 0.01f;
        var pos_low = origin + new Vector3(80 * (1 + i), (line.low - lowest) / ydelta * 80, 0) * 0.01f;
        mBody.transform.position = (pos_close + pos_open)/ 2f;
        mBody.rectTransform.sizeDelta = new Vector2(30, 80*body/ydelta);
        mUp.rectTransform.sizeDelta = new Vector2(4, (line.high - centerw) * 80 / ydelta);
        mDown.rectTransform.sizeDelta = new Vector2(4, (centerw - line.low) * 80 / ydelta);
    }


    public void SetData1(KLineData line, int i, Vector2 origin, float xfactor, float yfactor, float lowest)
    {
        var centerw = (line.open + line.close) / 2f;
        var is_long = line.close > line.open;
        var body = Mathf.Abs(line.close - line.open);
        mBody.color = is_long ? Color.red : Color.green;
        mUp.color = is_long ? Color.red : Color.green;
        mDown.color = is_long ? Color.red : Color.green;

        var pos_close = origin + new Vector2(xfactor * (1 + i), (line.close - lowest) *yfactor);
        var pos_open = origin + new Vector2(xfactor * (1 + i), (line.open - lowest) * yfactor);
        var pos_high = origin + new Vector2(xfactor * (1 + i), (line.high - lowest) * yfactor);
        var pos_low = origin + new Vector2(xfactor * (1 + i), (line.low - lowest) * yfactor);
        transform.position = (pos_close + pos_open) / 2f + Vector2.left * xfactor*0.5f;
        mBody.rectTransform.sizeDelta = new Vector2(xfactor*0.9f, body *yfactor);

        var shadoww = Mathf.Clamp(xfactor * 0.1f,1,4);
        mUp.rectTransform.sizeDelta = new Vector2(shadoww, (line.high - centerw) * yfactor);
        mDown.rectTransform.sizeDelta = new Vector2(shadoww, (centerw - line.low) * yfactor);
    }
}
